<template>
  <div>
    <el-form
      :model="form"
      :rules="rules"
      label-width="150px"
      ref="form"
      class="kylin-form"
    >
      <el-form-item :label="$t('configure.configure_group_name')" prop="name">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item :label="$t('configure.normalLibrary')" prop="standardId">
        <el-select v-model="form.standardId" size="small" class="size" disabled>
          <el-option
            v-for="item in normals"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('configure.configure_item')" prop="configure">
        <virtual-list
          :value="form.configure"
          size="small"
          class="size"
          :multiple="true"
          :list="configures"
          @selectValue="handleSelectConfigure"
          :obj="true"
        ></virtual-list>
      </el-form-item>
      <el-form-item :label="$t('description')" prop="description">
        <el-input v-model="form.description" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">{{ $t("cancel") }}</el-button>
      <el-button class="kylin-button" @click="handleOk">{{
        $t("save")
      }}</el-button>
    </div>
  </div>
</template>

<script>
import { getStandardLib, getConfigureItem } from "@/api/config";
import virtualList from "@/components/virtualList";
import {
  updateConfigureGroup,
  getOneConfigureGroup,
} from "@/api/configure_group";
export default {
  components: {
    virtualList,
  },
  data() {
    return {
      form: {
        name: "",
        standardId: "",
        configure: [],
        ascription: "",
        description: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: this.$t("configure.configure_group_name_require"),
            trigger: "change",
          },
        ],
        standardId: [
          {
            required: true,
            message: this.$t("configure.normal_require"),
            trigger: "change",
          },
        ],
        configure: [
          {
            required: true,
            message: this.$t("configure.normal_require"),
            trigger: "change",
          },
        ],
        ascription: [
          {
            required: true,
            message: this.$t("configure.ascription_require"),
            trigger: "change",
          },
        ],
      },
      normals: [],
      configures: [],
      ascriptions: [
        { id: "中心", name: "中心" },
        { id: "基地", name: "基地" },
        { id: "部门", name: "部门" },
      ],
    };
  },
  props: {
    row_id: {
      type: String | Number,
    },
  },
  methods: {
    handleSelectConfigure(value) {
      this.form.configure = value;
    },
    handleCancel() {
      this.$refs.form.resetFields();
      this.$emit("click");
    },
    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          updateConfigureGroup({
            groupName: this.form.name,
            configIds: this.form.configure,
            groupId: this.row_id,
            standardLibrary: this.form.standardId,
            description: this.form.description
          }).then((res) => {
            if (res.data.code === "0") {
              this.$message({
                type: "success",
                message: res.data.message,
              });
              this.$emit("click", "success");
              this.$refs.form.resetFields();
            } else {
              this.$message({
                type: "error",
                message: res.data.message,
              });
            }
          });
        }
      });
    },
  },
  mounted() {
     getStandardLib({ paged: false }).then((res) => {
      res.data.data.results.map((item) => {
        this.normals.push({
          id: item.id,
          name: `${item.name}_${item.version}_${item.cpuArch}`,
        });
      });
    });
    getOneConfigureGroup(this.row_id).then((res) => {
      let data = res.data.data;
      this.form.name = data.groupName;
      this.form.standardId = parseInt(data.standardLibrary);
      this.form.configure = data.configIds;
      this.form.description = data.description;
    });
  },
  watch: {
    "form.standardId": function (newValue) {
      getConfigureItem({ standardId: newValue, paged: false }).then((res) => {
        this.configures = res.data.data.results;
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.size {
  width: 100%;
}
</style>